package com.ttbj.controller.order;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ttbj.constant.order.StateEnum;
import com.ttbj.product.api.OrderFacade;
import com.ttbj.product.bean.dto.OrderDto;
import com.ttbj.product.bean.dto.QRCodeOrderDto;
import com.ttbj.product.bean.dto.UserDto;
import com.ttbj.product.bean.dto.ViewData;
import com.ttbj.util.Assert;
import com.ttbj.util.ViewModelUtil;

@Controller
@RequestMapping("/applet/order")
public class DefaultOrderController {

	@Autowired
	private  OrderFacade orderFacade = null; 
	
	
	/**
	 * 订单列表
	 * @param request
	 * @param openId
	 * @return
	 */
	@RequestMapping("/all.do")
	@ResponseBody
	 public ViewData orderAll(HttpServletRequest request,String openId) {
		UserDto userDto = (UserDto) request.getAttribute("user");
		 List<OrderDto> list =  orderFacade. orderAll(userDto);
		 if(!Assert.isEmpty(list)){
			 return ViewModelUtil.buildSuccessPage("成功", list, list.size());
		 }
		 return ViewModelUtil.buildSuccess("成功");
	 }
	 
	
	/**
	 * 订单详情
	 * @param request
	 * @param orderId
	 * @param openId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/detail.do")
	@ResponseBody
	 public ViewData orderDetail(HttpServletRequest request,Long orderId,String openId) throws Exception{
		 OrderDto dto =  orderFacade.complexDetailOrder(orderId);
		return ViewModelUtil.buildSuccess("成功", dto);
	 }
	 
	
	
	/**
	 * 扫码获取订单信息
	 * @param openId
	 * @param qr
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/code/info.do")
	@ResponseBody
	 public ViewData orderCodeInfo(String openId,String qr) throws Exception{
		QRCodeOrderDto dto =  orderFacade.getOrderCodeInfo(qr);
		return ViewModelUtil.buildSuccess("成功", dto);
	 }
	
	/**
	 * 用户确认订单接口
	 */
	@RequestMapping("/confirm.do")
	@ResponseBody 
	public ViewData orderConfirm(String openId,Long orderId) throws Exception{
		orderFacade.updateState(orderId, StateEnum.ORDER_ACKNOW.getCode());
		return ViewModelUtil.buildSuccess("成功");
	}
	
	
	/**
	 * 用户退单
	 */
	@RequestMapping("/refund.do")
	@ResponseBody
	public ViewData orderRefund(String openId,Long orderId) throws Exception{
		orderFacade.orderRefund(orderId);
		return ViewModelUtil.buildSuccess("成功");
	}
	 
	 
	 
}
